package com.heatup.apt.api.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.heatup.apt.api.service.DataDictService;

import com.heatup.apt.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.heatup.apt.common.dao.DataDictMapper;
import com.heatup.apt.common.util.DateUtils;
import com.heatup.apt.model.DataDict;

/**
 * Created by b3st9u on 16/9/19.
 */
@Service
public class DataDictServiceImpl implements DataDictService {
	
	Logger logger = LoggerFactory.getLogger(DataDictServiceImpl.class);
	
    @Autowired
    DataDictMapper dataDictMapper;

    @Override
    public Map<String, Object> queryDictMap(String dictType) {
     
        List<DataDict> list =  dataDictMapper.queryDataDictByDictTypeAndStatus(dictType, "1");
        Map<String, Object> map =null;
        if (list != null && list.size() > 0) {
            map = new HashMap<String, Object>();
            for(DataDict dict:list){
                map.put(dict.getDictCode(),dict.getDictName());
            }
        }
        return map;
    }

	@Override
	public List<DataDict> queryDataDicts(String dictType) {
		return dataDictMapper.queryDataDictByDictTypeAndStatus(dictType, "1");
	}

	@Override
	public DataDict queryDataDictByDictTypeAndDictCode(String dictType, String dictCode) {
		DataDict dataDict = new DataDict();
		dataDict.setDictType(dictType);
		dataDict.setDictCode(dictCode);
		dataDict = dataDictMapper.selectOne(dataDict);
		if(dataDict == null) {
			return null;
		}
		
		return dataDict;
	}

	@Override
	public Date getBookRetailBeginTime() {
		Date retailBeginTime = null;
		try {
			// 获取零售开始时间
			List<DataDict> dataDictList = dataDictMapper.queryDataDictByDictTypeAndStatus("RETAIL_BEGIN_TIME", "1");
			if(dataDictList.size() > 0) {
				DataDict dataDict = dataDictList.get(0);
				retailBeginTime = DateUtils.toDate(dataDict.getDictCode());
			}
		} catch (Exception e) {
			// TODO: handle exception
			logger.error(" getRetailBeginTime | error:"+e.toString()+e.getCause(),e);
		}
		return retailBeginTime;
	}

	@Override
	public Date getMealRetailBeginTime() {
		Date retailBeginTime = null;
		try {
			// 获取零售开始时间
			List<DataDict> dataDictList = dataDictMapper.queryDataDictByDictTypeAndStatus("MEAL_RETAIL_BEGIN_TIME", "1");
			if(dataDictList.size() > 0) {
				DataDict dataDict = dataDictList.get(0);
				retailBeginTime = DateUtils.toDate(dataDict.getDictCode());
			}
		} catch (Exception e) {
			// TODO: handle exception
			logger.error(" getRetailBeginTime | error:"+e.toString()+e.getCause(),e);
		}
		return retailBeginTime;
	}

	@Override
	public List<DataDict> queryDataDictCodeByDictTypeAndStatus(String dictType, String status) {
		return dataDictMapper.queryDataDictCodeByDictTypeAndStatus(dictType,status);
	}

	@Override
	public Map<String, Object> queryCoachAdvisoryTime(Date date) {
    	// step 1 先查询出开始和结束的时间
    	List<DataDict> list = dataDictMapper.queryDataDictCodeByDictTypeAndStatus("COACH_ADVISORY_TIME","1");
    	String startTime = "";
    	String endTime = "";
    	String toDay = DateUtils.toDateString(date);

    	// step 2 设置开始和结束时间
    	for (int i = 0 ; i < list.size();i++){
    		if (StringUtil.isEmpty(startTime)){
				startTime = toDay + " " + list.get(i).getDictCode().trim();
			}else {
    			endTime = toDay + " " + list.get(i).getDictCode().trim();
    			break;
			}

		}
		try {

    		// 将时间最小的放在最前面
			Date startDateTime = DateUtils.toDate(startTime,DateUtils.MM);
			Date endDateTime = DateUtils.toDate(endTime,DateUtils.MM);
			if (startDateTime.compareTo(endDateTime) > 0){
				String time = startTime;
				startTime = endTime;
				endTime = time;
			}

			// 返回数据
			Map<String,Object> resultMap = new HashMap<>();
			resultMap.put("startTime",startTime);
			resultMap.put("endTime",endTime);
			return resultMap;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
}
